package pers.xiaojun.boot.module.system.controller.dept;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pers.xiaojun.boot.mybatis.core.convert.PageConvert;
import pers.xiaojun.boot.common.pojo.CommonResult;
import pers.xiaojun.boot.common.pojo.PageResult;
import pers.xiaojun.boot.common.util.BeanUtils;
import pers.xiaojun.boot.excel.util.ExcelUtils;
import pers.xiaojun.boot.module.system.controller.dept.vo.post.PostQueryReqVO;
import pers.xiaojun.boot.module.system.controller.dept.vo.post.PostRespVO;
import pers.xiaojun.boot.module.system.controller.dept.vo.post.PostSaveReqVO;
import pers.xiaojun.boot.module.system.controller.dept.vo.post.PostSimpleRespVO;
import pers.xiaojun.boot.module.system.service.dept.PostService;
import pers.xiaojun.boot.web.core.annotation.AdminApi;

import java.io.IOException;
import java.util.List;

/**
 * 岗位控制层
 *
 * @author xiaojun
 * @since 2025-11-02
 */
@Tag(name = "岗位")
@AdminApi
@RestController
@RequestMapping("/system/post")
@RequiredArgsConstructor
public class PostController {

    private final PostService postService;

    @PostMapping
    @Operation(summary = "创建岗位")
    @PreAuthorize("@ss.hasPermission('system:post:create')")
    public CommonResult<?> createPost(@Valid @RequestBody PostSaveReqVO createVO) {
        return CommonResult.success(postService.savePost(createVO).getId());
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新岗位")
    @PreAuthorize("@ss.hasPermission('system:post:update')")
    @Parameter(name = "id", description = "岗位Id", required = true)
    public CommonResult<?> updatePost(@PathVariable("id") Long postId, @Valid @RequestBody PostSaveReqVO updateVO) {
        updateVO.setId(postId);
        return CommonResult.success(postService.updatePost(updateVO).getId());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除岗位")
    @PreAuthorize("@ss.hasPermission('system:post:delete')")
    @Parameter(name = "id", description = "岗位Id", required = true)
    public CommonResult<?> deletePost(@PathVariable("id") Long postId) {
        postService.removePostById(postId);
        return CommonResult.success();
    }

    @DeleteMapping("/batch/{ids}")
    @Operation(summary = "删除一批岗位")
    @PreAuthorize("@ss.hasPermission('system:post:delete')")
    @Parameter(name = "ids", description = "岗位Id列表", required = true)
    public CommonResult<?> deletePostBatch(@PathVariable("ids") List<Long> postIds) {
        postService.removePostByIds(postIds);
        return CommonResult.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取岗位信息")
    @PreAuthorize("@ss.hasPermission('system:post:query')")
    @Parameter(name = "id", description = "岗位Id", required = true)
    public CommonResult<PostRespVO> getPost(@PathVariable("id") Long posId) {
        return CommonResult.success(BeanUtil.toBean(postService.getPostById(posId), PostRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获取岗位分页")
    @PreAuthorize("@ss.hasPermission('system:post:query')")
    public CommonResult<PageResult<PostRespVO>> getPostPage(@Valid PostQueryReqVO queryVO) {
        return CommonResult.success(PageConvert.INSTANCE.toPageResult(postService.listPostsPage(queryVO), PostRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获取简要岗位列表")
    @PreAuthorize("@ss.hasPermission('system:post:query')")
    public CommonResult<List<PostSimpleRespVO>> getSimplePostList() {
        return CommonResult.success(BeanUtils.toBeans(postService.list(), PostSimpleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出岗位数据")
    @PreAuthorize("@ss.hasPermission('system:post:export')")
    public void exportPostExcel(HttpServletResponse response) throws IOException {
        ExcelUtils.write(
                response,
                ExcelUtils.toXlsxFilename("岗位数据"),
                "岗位数据",
                PostRespVO.class,
                BeanUtils.toBeans(postService.list(), PostRespVO.class)
        );
    }

}
